package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.Area;
import com.vehicle.bean.ParkingLot;
import com.vehicle.mapper.AreaMapper;
import com.vehicle.mapper.ParkingLotMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.ParkingLotService;
import com.vehicle.util.LogUtil;
import com.vehicle.vo.ListParkingVO;
import com.vehicle.vo.ParkingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-21
 */
@Service
public class ParkingLotServiceImpl extends ServiceImpl<ParkingLotMapper, ParkingLot> implements ParkingLotService {

    @Autowired
    ParkingLotMapper parkingLotMapper;

    @Autowired
    AreaMapper areaMapper;

    @Autowired
    UserLogMapper userLogMapper;

    @Override
    public ListParkingVO getAllParkingList(Integer page, Integer size) {
        Page<ParkingLot> infoPage = new Page<>(page,size);
        Page<ParkingLot> parkInfoPage = parkingLotMapper.selectPage(infoPage,null);
        ListParkingVO listParkingVO = new ListParkingVO();
        listParkingVO.setSize(parkInfoPage.getSize());
        listParkingVO.setTotal(parkInfoPage.getTotal());
        List<ParkingLot> parkingLots = parkInfoPage.getRecords();
        List<ParkingVO> parkingVOS = new ArrayList<>();
        System.out.println("所有的parkinglot为："+parkingLots);
        for (ParkingLot p:parkingLots) {
            ParkingVO parkingVO = new ParkingVO();
            parkingVO.setParkingId(p.getParkingId());
            if(p.getSeatState() == 0){
                parkingVO.setParkingState("空闲");
            }
            if(p.getSeatState() == 1){
                parkingVO.setParkingState("占有");
            }
            QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<Area>();
            areaQueryWrapper.eq("area_id",p.getAreaId());
            Area area = areaMapper.selectOne(areaQueryWrapper);
            BeanUtils.copyProperties(area,parkingVO);
            parkingVOS.add(parkingVO);
            System.out.println("每一个parkingVO为："+parkingVO);
        }
        listParkingVO.setContent(parkingVOS);
        System.out.println("在最最最最后面得parkingVOS为："+parkingVOS);
        return listParkingVO;
    }

    @Override
    public String getParkingLocation(Integer parkingId) {
        ParkingLot p = parkingLotMapper.selectById(parkingId);
       Area area = areaMapper.selectById(p.getAreaId());
       return area.getLocation();
    }

    @Override
    public List<ParkingVO> getParkingByContent(String content) {
        QueryWrapper<ParkingLot> parkingLotQueryWrapper = new QueryWrapper<>();
        List<ParkingLot> parkingLots = parkingLotMapper.selectList(parkingLotQueryWrapper);
        List<ParkingVO> parkingVOS = new ArrayList<>();
        System.out.println("所有的parkinglot为："+parkingLots);
        QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<Area>();
        areaQueryWrapper.like("location",content);
        List<Area> areas = areaMapper.selectList(areaQueryWrapper);
        for (Area a:areas) {
            for (ParkingLot p:parkingLots) {
                if(a.getAreaId() == p.getAreaId()){
                    ParkingVO parkingVO = new ParkingVO();
                    parkingVO.setParkingId(p.getParkingId());
                    if(p.getSeatState() == 0){
                        parkingVO.setParkingState("空闲");
                    }
                    if(p.getSeatState() == 1){
                        parkingVO.setParkingState("占有");
                    }
                    BeanUtils.copyProperties(a,parkingVO);
                    parkingVOS.add(parkingVO);
                    System.out.println("每一个parkingVO为："+parkingVO);
                }
            }
        }
        System.out.println("在最最最最后面得parkingVOS为："+parkingVOS);
        return parkingVOS;
    }

    @Override
    public Boolean updateParkingState(String ip,Integer id,Integer parkingId, Integer state) {
        boolean b = parkingLotMapper.updateState(parkingId,state);
        if(b){
            userLogMapper.insert(LogUtil.insertAdminUpdate(id,"修改车位状态成功",ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminUpdate(id,"修改车位状态失败",ip,"失败"));
        return false;
    }

}
